package com.pda.xinmazhi.presenter;

import com.pda.xinmazhi.bean.ScanResultBean;
import com.pda.xinmazhi.bean.UserBean;
import com.pda.xinmazhi.bean.request.QRcodeRequest;
import com.pda.xinmazhi.bean.request.RawInRequest;
import com.pda.xinmazhi.contract.DunTongInContract;
import com.pda.xinmazhi.net.ArticleBean;
import com.pda.xinmazhi.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import java.util.List;

import io.reactivex.functions.Consumer;

public class DunTongInPresenter extends BasePresenter<DunTongInContract .View>implements DunTongInContract.Presenter {

    @Override
    public void getArticleList() {
        NetUtils.geApi().getArticleList()
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<ArticleBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<ArticleBean>> listBaseAppEntity) throws Exception {
                        getView().setList(listBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
    @Override
    public void scanTheQRCodeToVerify(String code) {
        QRcodeRequest request = new QRcodeRequest();
        request.setQRCode(code);
        NetUtils.geApi().scanTheQRCodeToVerify(request)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<ScanResultBean>>() {
                    @Override
                    public void accept(BaseAppEntity<ScanResultBean> scanResultBeanBaseAppEntity) throws Exception {
                        getView().setScanBean(scanResultBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
    @Override
    public void callRawIn(RawInRequest request) {
        NetUtils.geApi().callRawIn(request)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<UserBean>>() {
                    @Override
                    public void accept(BaseAppEntity<UserBean> userBeanBaseAppEntity) throws Exception {
                        getView().showMessage(userBeanBaseAppEntity.getMsg());
                        getView().onSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
